<template>
  <view class="pb-20">
    <template v-if="dataList && dataList.length">
      <warn-item class="item" v-for="(item, index) in dataList" :key="index" :data="item" :index="index" />
    </template>
    <view v-else>
      <Empty />
    </view>
  </view>
</template>

<script>
import Empty from '@/components/cu-empty/index.vue'
import warnItem from './components/warnItem.vue'
import { getWarnEventList } from '@/api/warn-event/index.js'

export default {
  components: {
    Empty,
    warnItem
  },
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      dataList: [],
      total: 0
    }
  },
  onShow: function () {
    this.pageNum = 1
    this.getList()
  },
  onPullDownRefresh() {
    this.pageNum = 1
    this.getList()
  },
  onReachBottom() {
    if (this.pageNum * this.pageSize < this.total) {
      this.pageNum++
      this.getList(false)
    }
  },
  methods: {
    async getList(isRefresh = true) {
      const { pageNum, pageSize } = this
      const params = {
        pageNum,
        pageSize,
        enableUser: true,
        enableNoHandler: true
      }
      const { rows, total } = await getWarnEventList(params).finally(() => uni.stopPullDownRefresh())
      if (isRefresh) {
        this.dataList = rows || []
      } else {
        this.dataList = [...this.dataList, ...rows]
      }
      this.total = total
    }
  }
}
</script>

<style lang="scss" scoped>
.item {
  margin-top: 20rpx;
  margin-bottom: 30rpx;
}
</style>
